var newCompanyId;
var auditStatus;
$(function() {
	//审核状态
	sys.selectpicker({
		id : 'selAuditStatus',
		url : rootPath + '/dict/listKeyMapEntites',
		param : {
			keyType : "customerAuditStatus"
		},
		valueField : 'keyValue',
		textField : 'keyIntro',
		defaultValue: '0',
		defaultAll : true,
		loadSuccess: function(){
			initCustomerGrid();
		}
	})
	
    //申请开始时间
    sys.datepicker({id: 'startCreatTime', type:'datetime'});
	$('#startCreatTime').val('');
	//申请结束时间
	sys.datepicker({id: 'endCreatTime', type:'datetime'});
	$('#endCreatTime').val('');
	$('.datetime .fa-calendar').on('click', function(){
		$(this).prev().click();
	})
	
	// 查询
	$('#btnQuery').on('click', function() {
		sys.reloadTable('customerTable');
		refreshAppendingApproveCount();
	});
	
	// 清空
	$('#btnClear').on('click', function() {
		$("#customerName").val(""); 
		$("#mobile").val(""); 
		$('#startCreatTime').val('');
		$('#endCreatTime').val('');
		$('#selAuditStatus').selectpicker('val', '0');
	});
	
})

/**
 * 初始化用户信息列表
 */
function initCustomerGrid(status){
	// 定义列
	var columns = [
		{ ield : 'no', title : '序号', align : 'left', valign:'middle', 
			formatter : function(value, row, index) {
				return index + 1;
			}
		},
		{ field : 'mobile', title : '手机号码', align : 'center', valign:'middle' },
		{ field : 'userName', title : '用户名称', align : 'center', valign:'middle' },
		{ field : 'companyInfo.companyName', title : '客户单位', align : 'center', valign:'middle' }, 
		{ field : '#', title : '审核状态', align : 'center', valign:'middle',
			formatter : function(value, row, index) {
				var strHtml = ""; 
				if (row.status == '0') {
					strHtml += "<span id='status_"+row.id+"' style='color:#FAA74D;'>待审核</span>";
				} else if (row.status == '1') {
					strHtml += "<span id='status_"+row.id+"' style='color:#52B747;'>通过</span>";
				} else  if (row.status == '2') {
					strHtml += "<span id='status_"+row.id+"' style='color:#F74E18;'>审核不通过</span>";
				}
				return strHtml;
			}
		},
		{ field : '#', title : '操作', align : 'center', valign:'middle', 
			formatter : function(value, row, index) {
				var strHtml = "";
				if (row.status == '0') {
					strHtml += "<a id='audit_"+row.id+"' class='btn btn-warning btn-xs' onclick='toAduit("+row.id+")'>审核</a>";
				}
				strHtml += "<a class='btn btn-primary btn-xs' onclick='toChangeCompany("+row.id+")'>归属单位</a>";
				strHtml += "<a class='btn btn-info btn-xs' onclick='toViewCustomerInfo("+row.id+")'>详情</a>";
				return strHtml;
			}
		}
	];
	
	// 定义查询回调函数（查询参数在此设置）
	var queryParams = function(params) {
		var approvalStatus = $('#selAuditStatus').selectpicker('val');
		return {
			rows : params.limit, // 页面大小
			page : params.pageNumber, // 页码 
			"userName": $("#customerName").val(),
			"status": approvalStatus,
			"startCreateTime": $("#startCreateTime").val(),
			"endCreateTime": $("#endCreateTime").val(),
			"companyInfo.companyName"	: $("#companyName").val(),  
			"mobile"		: $("#mobile").val(), 
		};
	};
	
	var url = rootPath + "/customer/listCustomerPage"; // 请求json数据的服务器地址
	sys.initTable('customerTable', {
		url : url,
		queryParams : queryParams,
		columns : columns,
		striped : true,
		clickToSelect : true
	});
}

/**
 * 审核 
 */
function toAduit(id){
	$('#layer_opt_status').val(''); 
	var url = rootPath + '/registerMgmt/toAuditPage?customerId='+id;
	showDialogUrl('审核', 600, 380, url, function(index){
		var optStatus = $('#layer_opt_status').val();
		if (optStatus == '1') { 
			if (auditStatus == '1') {
				$('#status_'+id).css({color:'#52B747'}).html("通过");
			} else if (auditStatus == '2') {
				$('#status_'+id).css({color:'#F74E18'}).html("审核不通过");
			}
			//更改菜单中的提示信息
			refreshAppendingApproveCount();
			
			$('#audit_'+id).remove();
		}
		layer.close(index);
	})
	
}

//刷新菜单中的待审核人数信息
function refreshAppendingApproveCount(){
	window.parent.$('.pendingApproval').show();
	$.ajax({
		type: 'POST',
		url: rootPath + "/customer/getPendingApprovalCount",
		dataType: 'JSON',
		success: function(result){
			if (result.resultCode == '1') {
				if (result.count == '0') {
					//左侧菜单
					window.parent.$('.pendingApproval').hide();
				} else {
					//左侧菜单
					window.parent.$('.pendingApproval').html(result.count);
					//眉头
					window.parent.$('#to_approve_customer').find('.badge').html(result.count);
				}
				
			}
		}
	})
}

/**
 * 更换归属单位 
 */
function toChangeCompany(id){
	$('#layer_opt_status').val(''); 
	var url = rootPath + '/company/toSelectCompanyPage';
	var index = showDialogUrl('选择公司', 1000, 520, url, function(index){
		var optStatus = $('#layer_opt_status').val();
		if (optStatus == '1') { 
			$.ajax({
				type: 'POST',
				url: rootPath + "/customer/saveOrUpdateCustomer",
				data: {id: id, companyId: newCompanyId},
				dataType: 'JSON',
				success: function(result){
					if (result.code == '1') {
						showSuccess("变更成功!");
						sys.reloadTable('customerTable');
					}
				}
			})
		}
		layer.close(index);
	})
	layer.full(index);
}

/**
 * 查看客户详情 
 */
function toViewCustomerInfo(id){
	$('#layer_opt_status').val(''); 
	var url = rootPath + '/registerMgmt/toCustomerDetailPage?customerId='+id;
	var index = showDialogUrl('详情', 1000, 480, url, function(index){
		var optStatus = $('#layer_opt_status').val();
		if (optStatus == '1') { 
			if (auditStatus == '1') {
				$('#status_'+id).css({color:'#52B747'}).html("通过");
			} else if (auditStatus == '2') {
				$('#status_'+id).css({color:'#F74E18'}).html("审核不通过");
			}
			$('#audit_'+id).remove();
		}
		layer.close(index);
	})
	layer.full(index);
}